package com.ufenlei.LTS {
	import flash.events.ProgressEvent;	
	
	import com.ufenlei.LTS.Block;
	import com.ufenlei.tool.RoundRectangle;

	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;

	/**
	 * @author bbn
	 */
	public class TestingLib extends Sprite{
		private var mainBlock:ScrolledBlock;
		private var sideBlock:ScrolledBlock;
		private var border:RoundRectangle;
		private var borderWidth:uint = 4;
		private var libItemList:Array = new Array();
		private var libItemHeight:Array = new Array();
		private var listurl:String;
		private var libitemurl:String;
		private var listLoader:URLLoader;
		private var _paperLoaded:Boolean = false;
		private var libItemLoader:URLLoader;
		
		public function TestingLib(bwidth : uint, bheight : uint,mainWidth:uint) {
			mainBlock = new ScrolledBlock(mainWidth,bheight);
			sideBlock = new ScrolledBlock(bwidth - mainWidth - borderWidth, bheight);
			border = new RoundRectangle(borderWidth,bheight,0,0xffffff);			
			addChild(mainBlock);
			addChild(sideBlock);
			//addContent(border);
			border.x = bwidth - mainWidth - borderWidth;
			border.y = 0;
			mainBlock.y = 0;
			mainBlock.x = bwidth - mainWidth;
			sideBlock.x = 0;
			sideBlock.y = 0;
			mainBlock.addEventListener(Input.CHANGE, onMainBlockChange,true);
		}
		public function load(listURL:String = null):Boolean{
			trace("loadLib",listURL);
			if(!listurl&&listURL == null){
				return false;
			}
			if(!listurl){
				listurl = listURL;
			}
			var myXMLURL:URLRequest = new URLRequest(listurl);
			listLoader = new URLLoader();
			listLoader.addEventListener("complete", listLoaded);
			listLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			listLoader.addEventListener(Event.OPEN, onLoaderOpen);
			listLoader.load(myXMLURL);
			return true;
		}
		private function onLoaderOpen(evt:Event):void{
			if(evt.target == listLoader){
				listLoader.addEventListener(ProgressEvent.PROGRESS,onLoaderProgress);
			}
			if(evt.target == libItemLoader){
				libItemLoader.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
			}
			this.dispatchEvent(evt.clone());
			
		}
		private function onIOError(evt:Event):void{
			trace("ioerror");
			if(evt.target == listLoader){
				this.load();
			}
			if(evt.target == listLoader){
				this.loadLibItem();
			}
		}
		public function onLoaderProgress(evt:ProgressEvent):void{
			this.dispatchEvent(evt.clone());
		}
		private function listLoaded(evt:Event):void{
			trace("loaded Lib");
			listLoader.removeEventListener(ProgressEvent.PROGRESS,onLoaderProgress);
			listLoader.removeEventListener(Event.OPEN, onLoaderOpen);
			listLoader.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
			this.dispatchEvent(evt.clone());
			var myXML:XML = new XML((evt.target as URLLoader).data);
			trace(myXML.toString());
			var testingLib:ListItem = new TestingLibListItem("试题库");
			for each(var lib:XML in myXML.lib){
				var libItem:TestingListItem = new TestingListItem(lib.@name,lib.@source);
				testingLib.addChildItem(libItem);
			}
			sideBlock.addContent(testingLib);
			testingLib.addEventListener(Input.CLICK, onLibClick);
		}
		private function onLibClick(evt:Event):void{
			if( (evt.target is TestingListItem) && (evt.target as TestingListItem).url){
				loadLibItem((evt.target as TestingListItem).url);
			}
		}
		private function loadLibItem(s:String = null):void{
			trace("loading LibItem",s);
			if( (s == null && !libitemurl) || (s == this.libitemurl && _paperLoaded )){
				return;
			}
			_paperLoaded = false;
			this.libitemurl = s;
			var liburl:URLRequest = new URLRequest(libitemurl);
			libItemLoader = new URLLoader();
			libItemLoader.addEventListener(Event.COMPLETE, onLibItemLoaded);
			libItemLoader.addEventListener(Event.OPEN, onLoaderOpen);
			libItemLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			libItemLoader.load(liburl);
		}
		private function onLibItemLoaded(evt:Event):void{
			_paperLoaded = true;
			libItemLoader.removeEventListener(Event.COMPLETE, onLibItemLoaded);
			libItemLoader.removeEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
			libItemLoader.removeEventListener(Event.OPEN, onLoaderOpen);
			libItemLoader.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
			this.dispatchEvent(evt.clone());
			mainBlock.clear();
			mainBlock.scrollTo(0);
			libItemList = [];
			libItemHeight = [];
			var myxml:XML = new XML((evt.target as URLLoader).data);
			trace(myxml);
			for each(var item:XML in myxml.child("item")){
				var libitem:TestingLibItem = new TestingLibItem(item.@title,item.@source,"0-0-0");
				libItemList.push(libitem);
				libItemHeight.push(libitem.height);
				mainBlock.addContent(libitem,5);
			}
		}
		private function onMainBlockChange(evt:Event):void{
			if(evt.target is TestingLibItem){
				trace("onMainBlockChange");
				var tli:TestingLibItem = (evt.target as TestingLibItem);
				var index:int = libItemList.indexOf(tli);
				var change:int = tli.height - libItemHeight[index];
				libItemHeight[index] = tli.height;
				for (var i:int = index+1; i<libItemList.length; i++){
					libItemList[i].y += change;
				}
			}
		}
	}
}
